package com.mobisystems.youpark;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.mobisystems.youpark.settings.BankAccountActivity;
import com.mobisystems.youpark.settings.CarInformationActivity;
import com.mobisystems.youpark.settings.CreditCardActivity;
import com.mobisystems.youpark.settings.CreditCardActivity.ProcessCardInformation;
import com.parse.ParseUser;
import com.stripe.Stripe;
import com.stripe.exception.APIConnectionException;
import com.stripe.exception.APIException;
import com.stripe.exception.AuthenticationException;
import com.stripe.exception.CardException;
import com.stripe.exception.InvalidRequestException;
import com.stripe.model.Token;

import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SettingsActivity extends Activity {
	private ListView listView;
	private String[] settingListItems;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		Stripe.apiKey = "sk_test_waGgPKE1zA91Nru33emcGIUL";
		//using custom action bar
		ActionBar customActionBar = getActionBar();
		customActionBar.setDisplayShowHomeEnabled(false);
		customActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.custom_actionbar, null);
        TextView titleTextView = (TextView) view.findViewById(R.id.title_text);
        titleTextView.setText(R.string.action_settings);
        view.findViewById(R.id.button_settings).setVisibility(View.INVISIBLE);
        view.findViewById(R.id.button_switch_modes).setVisibility(View.GONE);
		ImageButton backButton = (ImageButton) view.findViewById(R.id.button_back);
		backButton.setVisibility(View.VISIBLE);
		backButton.setBackgroundResource(R.drawable.back_button);
		backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				onBackPressed();
			}
		});
		customActionBar.setCustomView(view);
		customActionBar.setDisplayShowCustomEnabled(true);
        
        //user profile section
        ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser != null) {
        	Log.w("myLog", "ParseUser content Available");
        	((TextView)findViewById(R.id.textViewName)).setText((String)currentUser.get("fullName"));
            ((TextView)findViewById(R.id.textViewUsername)).setText((String)currentUser.getUsername());
        } else {
        	Log.w("myLog", "ParseUser content Null");
        }
        
        
		
		listView = (ListView) findViewById(R.id.settingsListView);
		settingListItems = getResources().getStringArray(R.array.settings_options);
		
		Typeface font = Typeface.createFromAsset(getAssets(),"fontawesome-webfont.ttf");
        
		
		// initializing adapter to fill with the values from string array
		ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(this,
																		android.R.layout.simple_list_item_1,
																		android.R.id.text1,
																		settingListItems);
		// setting the adapter to the list view
		listView.setAdapter(listViewAdapter);
		
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
				switch(position) {
					// Car information case
					case 0:
						Intent carInfoIntent = new Intent(SettingsActivity.this, CarInformationActivity.class);
						startActivity(carInfoIntent);
						break;
					// credit card case
					case 1:
						handleCreditCardClick();
						break;
					// bank account information case
					case 2:
						handleBankAccountClick();
						break;
					// log off case
					case 3:
						if(ParseUser.getCurrentUser() != null) {
							ParseUser.logOut();
							Toast.makeText(getApplicationContext(), "You have been logged off. :)", Toast.LENGTH_LONG);
							Intent signInIntent = new Intent(SettingsActivity.this, MainActivity.class);
							signInIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivity(signInIntent);
						} else {
							Intent signInIntent = new Intent(SettingsActivity.this, MainActivity.class);
							signInIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivity(signInIntent);
						}
						break;
					default:
						Toast.makeText(getApplicationContext(), "Please choose on of the options.", Toast.LENGTH_LONG).show();
				}
				
				//Toast.makeText(SettingsActivity.this, "Position is " + position + " and ID is " + id, Toast.LENGTH_LONG).show();
			}
		});
		
	}
	
	
	@SuppressWarnings("unchecked")
	protected void handleBankAccountClick(){
		ParseUser currentUser = ParseUser.getCurrentUser();
		String token = currentUser.getString("bankAccountToken");
		
		if(token != null){
				AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);
				builder.setMessage("Would you like to Replace it?");
				builder.setTitle("A Valid Bank Account Exists!");
				// Add the buttons
				builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				               // User clicked OK button
				        	   Intent bankAccountInfoIntent = new Intent(SettingsActivity.this, BankAccountActivity.class);
				        	   startActivity(bankAccountInfoIntent);
				           }
				       });
				builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
				           public void onClick(DialogInterface dialog, int id) {
				        	   dialog.cancel();
				           }
				       });
				
				// Create the AlertDialog
				AlertDialog dialog = builder.create();
				dialog.show();

		}
		
		else { // current user does not have a bank account
			Intent bankAccountInfoIntent = new Intent(SettingsActivity.this, BankAccountActivity.class);
			startActivity(bankAccountInfoIntent);
		}
		
		
	}
	

	
	
	protected void handleCreditCardClick(){
		
		
			// TODO Auto-generated method stub
			ParseUser currentUser = ParseUser.getCurrentUser();
			String token = currentUser.getString("creditCardToken");
			
			if(token != null){
					AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);
					builder.setMessage("Would you like to Replace it?");
					builder.setTitle("A Valid Credit Card Exists!");
					// Add the buttons
					builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					               // User clicked OK button
					        	   Intent creditCardInfoIntent = new Intent(SettingsActivity.this, CreditCardActivity.class);
									startActivity(creditCardInfoIntent);
					           }
					       });
					builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					        	   dialog.cancel();
					           }
					       });
					
					// Create the AlertDialog
					AlertDialog dialog = builder.create();
					dialog.show();

			}
			
			else { // current user does not have a bank account
				Intent creditCardInfoIntent = new Intent(SettingsActivity.this, CreditCardActivity.class);
				startActivity(creditCardInfoIntent);
			}
			
		
		

		
	}

	
}
